package com.andy.qhb.controller.game;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.andy.qhb.base.RespVO;
import com.andy.qhb.controller.GameController;
import com.andy.qhb.entity.UsrGroups;
import com.andy.qhb.game.GameUser;
import com.andy.qhb.game.Global;
import com.andy.qhb.game.RobotManager;
import com.andy.qhb.game.Room;
import com.andy.qhb.utils.GoConstant;
import com.andy.qhb.utils.JsonUtils;
import com.andy.qhb.vo.HFriend;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@RequestMapping(value="game/api")
public class Api126Controller extends GameController{
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	/**
	 * 群详情
	 * @param session
	 * @param data
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="126")
	public RespVO action(HttpSession session,String data) throws Exception{
		GameUser user1 = getUser(session);
		Map<String, Object> reqData = JsonUtils.json2Map(data);
		Long gid = MapUtils.getLong(reqData, "gid");
		
		if(gid >= GoConstant.LRROOM_ID_START) {
			// 路人房
			Room room = Global.getRoom(gid.intValue());
			int i=0;
			List<HFriend> membs = Lists.newArrayList();
			for(GameUser user:room.users()) {
				if(i++ >= 39) break;
				membs.add(new HFriend(user.getId(), user.getNickName(), user.getIcon()));
			}
			for(int j=i;j<39;j++) {
				GameUser robot = RobotManager.getRobots().get(j);
				membs.add(new HFriend(robot.getId(), robot.getNickName(), robot.getIcon()));
			}
			Map<String,Object> resp = Maps.newHashMap();
			resp.put("membs", membs);
			return RespVO.createSuccessJsonResonse(resp);
		}
		
		UsrGroups groups = Global.getUsrGroup(gid);
		if(groups == null) {
			return RespVO.createErrorJsonResonse("群不存在");
		}
		
		Map<String,Object> resp = Maps.newHashMap();
		resp.put("membs", groups.members());
		resp.put("cid", groups.getCid());
		
		return RespVO.createSuccessJsonResonse(resp);
		
	}
	
	
}